package com.ayz.gateway.center.infrastructure.notify;


import com.ayz.gateway.api.request.ApplicationConfig;
import com.ayz.gateway.api.response.GatewayCommonResult;
import com.ayz.gateway.api.utils.RequestConvertUtils;
import com.ayz.gateway.center.domain.repository.IApiRepository;
import com.ayz.gateway.common.constants.ApiData;
import com.ayz.gateway.common.constants.GatewayServiceData;
import com.ayz.gateway.common.notify.ApplicationEvent;
import com.ayz.gateway.common.notify.GenericApplicationListener;
import com.ayz.gateway.common.result.ResponseCode;
import com.google.protobuf.Any;
import io.grpc.stub.StreamObserver;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @ClassName GatewayServiceSubscriber
 * @Description TODO: 网关服务的订阅者 订阅一个网关组下API集合的变更事件
 * @Author 快乐的星球
 * @Date 2023/5/28 11:16
 * @Version 1.0
 **/
@Getter
@Setter
public class GatewayServiceSubscriber implements GenericApplicationListener {
    /**
     * 网关服务的基础设施类
     */
    private GatewayServiceData gatewayServiceData;

    private LinkedBlockingQueue<ApplicationEvent> applicationEventBlockingQueue;

    public GatewayServiceSubscriber(GatewayServiceData gatewayServiceData) {
        this.gatewayServiceData = gatewayServiceData;
        this.applicationEventBlockingQueue=new LinkedBlockingQueue<>();
    }
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        this.applicationEventBlockingQueue.offer(event);
    }

    @Override
    public boolean supportsEventType(Class<?> eventType) {
        if(eventType.isAssignableFrom(ApiDataChangeEvent.class)){
            return true;
        }else if(eventType.isAssignableFrom(GatewayServiceLogoutEvent.class)){
            return true;
        }

        return false;
    }

    public boolean isSubscribeGatewayGroup(ApplicationEvent applicationEvent){
        String gatewayGroup =(String) applicationEvent.getSource();
        if(gatewayGroup!=null && gatewayGroup.equals(gatewayServiceData.getGatewayGroup())){
            return true;
        }
        return false;
    }

    public void pushApiDataChange(StreamObserver<GatewayCommonResult.GatewayResult> responseObserver, IApiRepository apiRepository){
        //1:获取最新的网关组下 API集合列表信息
        List<ApiData> apiDataListForGroup = apiRepository.getApiDataListByGroup
                (gatewayServiceData.getGatewayGroup());
        //2:封装响应
        com.ayz.gateway.api.request.GatewayServiceSubscriber.SubscribeGatewayGroupResponse.Builder builder = com.ayz.gateway.api.request.GatewayServiceSubscriber.SubscribeGatewayGroupResponse.newBuilder();

        if(apiDataListForGroup!=null && !apiDataListForGroup.isEmpty()){
            for (ApiData apiData : apiDataListForGroup) {
                ApplicationConfig.ApiData grpcApiData = RequestConvertUtils.toGrpcApiData(apiData);
                builder.addApiData(grpcApiData);
            }
        }
        com.ayz.gateway.api.request.GatewayServiceSubscriber.SubscribeGatewayGroupResponse response = builder.build();

        GatewayCommonResult.GatewayResult.Builder resultBuilder = GatewayCommonResult.GatewayResult.newBuilder();
        GatewayCommonResult.GatewayResult gatewayResult = resultBuilder.setCode(ResponseCode.COMMON_SUCCESS).setData(Any.pack(response)).build();
        responseObserver.onNext(gatewayResult);
    }
}
